//
//  FileViewController.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/8.
//  Copyright © 2018年 longlong. All rights reserved.
//

#import "FileViewController.h"

#import "FileCollectionViewCell.h"
#import "FileCollectionViewFlowLayout.h"
#import "PhotosManager.h"

@interface FileViewController ()
<UICollectionViewDelegate,UICollectionViewDataSource>
{
    ///开始滑动时的 Y 值
    CGFloat _startSlidingY;
    ///向上滑动
    BOOL _upSliding;
    ///第一次运行
    BOOL _firstRun;
}
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) NSMutableArray<PHAsset *> *imageArray;
@end

@implementation FileViewController
static NSString * const reuseIdentifier = @"FileCollectionViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.collectionView];
    [self loadImageData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    if (_firstRun) {
        /*第一次运行显示界面时 所有显示的cell都放到最高层 后出现的就会压住前面的*/
        //上
        [collectionView bringSubviewToFront:cell];
    } else {
        
        if (_upSliding) {
            /* 向上滑时 后出现的就会压住前面的*/
            //上滑
            [collectionView bringSubviewToFront:cell];
        } else {
            /* 向下滑时 把每个将要出现的放到最底部 后出现的就会在前一个cell的下面*/
            //下滑
            [collectionView sendSubviewToBack:cell];
        }
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FileCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.imageAsset = self.imageArray[indexPath.item];
    return cell;
}

#pragma mark UIScrollViewDelegate
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _startSlidingY = scrollView.contentOffset.y;
    _firstRun=NO;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y<_startSlidingY) {
        //下
        _upSliding=NO;
    } else  {
        //上
        _upSliding=YES;
    }
    _startSlidingY = scrollView.contentOffset.y;
}

- (void)loadImageData
{
    __weak typeof(self) weakSelf = self;
    
    [HttpManager requestFileList];
}


#pragma mark - setter and getter

- (UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        FileCollectionViewFlowLayout *flatout = [[FileCollectionViewFlowLayout alloc] init];
        flatout.itemSize = CGSizeMake(CellWidth, CellWidth);
        
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth) collectionViewLayout:flatout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        
        [collectionView registerClass:[FileCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
        collectionView.alwaysBounceVertical = YES;
        collectionView.backgroundColor = [UIColor colorWithRed:38/255.0f green:44/255.0f blue:50/255.0f alpha:1];

        _collectionView = collectionView;
    }
    return _collectionView;
}

@end
